﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using com.cruxomatic.DTOs;
using com.cruxomatic.views.Administration;
using com.cruxomatic.controllers.Administration;
using com.cruxomatic.views.Enums;
using com.cruxomatic.DTOs.Configuration;
using com.cruxomatic.configuration;

public partial class Administration_ManageDepartments : BaseView, IManageDepartments
{
    ManageDepartmentsController _controller = null;
    protected FullGridPager fullGridPager;
    protected int MaxVisible = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        _controller = new ManageDepartmentsController(this);
        MaxVisible = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("MaximumPageCount").Value);
        grdDepartments.DataBound += new EventHandler(GridDataBound);
        _controller.InitView(IsPostBack);
        if (!IsPostBack)
        {
            #region Site Map
            SetSiteMap();
            #endregion
            SetPageSize();
            Page.DataBind();
        }
        if (IsPostBack)
        {
            fullGridPager = new FullGridPager(grdDepartments, MaxVisible, "Page", "of");
            fullGridPager.CreateCustomPager(grdDepartments.BottomPagerRow);
        }
    }

    #region Site Map
    private void SetSiteMap()
    {
        string query = Request.Url.Query;
        UrlTreeNodeItems.ManageDepartment = UrlTreeNodeItems.CreateNode("Manage Department",
                                                                          "/Administration/ManageDepartments.aspx" + query,
                                                                          "ManageDepartment",
                                                                          UrlTreeNodeItems.ManageMyOrganization);
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.ManageDepartment);
    }
    #endregion

    #region Paging

    protected void GridDataBound(object sender, EventArgs e)
    {
        if (fullGridPager == null)
        {
            fullGridPager = new FullGridPager(grdDepartments, MaxVisible, "Page", "of");
        }
        fullGridPager.CreateCustomPager(grdDepartments.BottomPagerRow);
        fullGridPager.PageGroups(grdDepartments.BottomPagerRow);
    }

    private void SetPageSize()
    {
        int PagerSize = Convert.ToInt32(CruxConfigurationManager.GetApplicationCustomSetting("PagerSize").Value);
        grdDepartments.PageSize = PagerSize;
    }

    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdDepartments.PageIndex = e.NewPageIndex;
        _controller.SwitchToListMode();
    }

    #endregion

    public void grdDepartments_RowCommand(object sender, CommandEventArgs e)
    {
        RowCommands rowCommand = (RowCommands)Enum.Parse(typeof(RowCommands), e.CommandName);
        if (rowCommand == RowCommands.EditItem)
        {
            string departmentId = e.CommandArgument.ToString();
            _controller.OpenDepartment(int.Parse(departmentId));
            UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Edit - " + CurrentDepartment.DepartmentName, UrlTreeNodeItems.ManageDepartment));
        }
        else if (rowCommand == RowCommands.DeleteItem)
        {
            string departmentId = e.CommandArgument.ToString();
            _controller.DeleteDepartment(int.Parse(departmentId));
        }

    }

    protected void grdDepartments_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton deleteButton = (ImageButton)e.Row.FindControl("btnItemDelete");
            string ReplaceConfirmation = new UILiterals().DeleteDepartmentConfirmation.Replace("{0}",
                DataBinder.Eval(e.Row.DataItem, "DepartmentName").ToString());
            deleteButton.Attributes.Add("onclick", "javascript:return "
                + "confirm('" + ReplaceConfirmation + "')");
        }
    }


    #region IManageDepartments Members

    public List<DepartmentInformation> Departments
    {
        get
        {
            return (List<DepartmentInformation>)grdDepartments.DataSource;
        }
        set
        {
            grdDepartments.DataSource = value;
            grdDepartments.DataBind();
        }
    }

    public DepartmentInformation CurrentDepartment
    {
        get
        {
            DepartmentInformation departmentInformation = new DepartmentInformation();
            departmentInformation.DepartmentID = int.Parse(lblDepartmentIDValue.Text);
            departmentInformation.DepartmentName = txtDepartmentName.Text;
            departmentInformation.DepartmentDescription = txtDepartmentDescription.Text;
            departmentInformation.DepartmentComments = txtDepartmentComments.Text;
            departmentInformation.CompanyID = CompanyID;
            departmentInformation.CompanyName = _controller.GetCompanyForDepartment(CompanyID).CompanyName;
            return departmentInformation;
        }
        set
        {
            lblDepartmentIDValue.Text = value.DepartmentID.ToString();
            txtDepartmentName.Text = value.DepartmentName;
            txtDepartmentDescription.Text = value.DepartmentDescription;
            txtDepartmentComments.Text = value.DepartmentComments;
        }
    }

    public FormMode FormMode
    {
        get
        {
            if (pnlList.Visible == true)
                return FormMode.List;
            else if (pnlAddEdit.Visible == true)
                return FormMode.AddEdit;
            else
                throw UIExceptions.InvalidFormModeException;
        }
        set
        {
            if (value == FormMode.List)
            {
                pnlList.Visible = true;
                pnlAddEdit.Visible = false;
            }
            else if (value == FormMode.AddEdit)
            {
                pnlList.Visible = false;
                pnlAddEdit.Visible = true;
            }
            else
            {
                throw UIExceptions.InvalidFormModeException;
            }
        }
    }

    #endregion
    protected void btnSave_Click(object sender, EventArgs e)
    {
        _controller.SaveDepartment();
        #region Site Map
        if (FormMode == FormMode.List)
            SetSiteMap();
        #endregion
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        _controller.CancelAddEdit();
        #region Site Map
        SetSiteMap();
        #endregion
    }
    protected void btnAddNewDepartment_Click(object sender, EventArgs e)
    {
        _controller.OpenDepartment(0);
        #region Site Map
        UrlTreeNodeItems.SetSiteNavigation(PageSiteMap, UrlTreeNodeItems.CreateLeafUrlTreeNode("Add", UrlTreeNodeItems.ManageDepartment));
        #endregion
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        _controller.RedirectToPreviousPage();
    }

    #region IManageDepartments Members


    public ErrorInformation ErrorDetails
    {
        get
        {
            return pnlError.ErrorInformation;
        }
        set
        {
            pnlError.ErrorInformation = value;
        }
    }

    #endregion

    #region IManageDepartments Members


    public int CompanyID
    {
        get { return int.Parse(Request.Params["id"].ToString()); }
    }

  
    #endregion
}
